var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    rmb: '666',
    list: [{
      code: "0001",
      name: "无人机",
      name2: '4100.00',
      url: "http://i1.mifile.cn/a1/pms_1487824962.01314237!220x220.jpg",
      style: "低配版",
      price: "4999.00",
      select: "circle",
      num: "1"
    },
    {
      code: "0002",
      name: "智能手环",
      name1: '黑色',
      name2: '4100.00',
      url: "http://i1.mifile.cn/a1/pms_1467962689.97551741!220x220.jpg",
      style: "2代",
      price: "149.00",
      select: "circle",
      num: "1"
    }, {
      code: "0003",
      name: "指环支架",
      name1: '黑色',
      name2:'4100.00',
      url: "http://i2.mifile.cn/a1/pms_1482221011.26064844.jpg",
      style: "金色",
      price: "19.00",
      select: "circle",
      num: "1"
    }],
    allSelect: "circle",
    num: 0,
    count: 0,
    hostUrl: app.globalData.hostUrl,

  },
  //改变选框状态
  change: function (e) {
    var that = this
    //得到下标
    var index = e.currentTarget.dataset.index
    //得到选中状态
    var select = e.currentTarget.dataset.select

    if (select == "2") {
      var stype = "1"
    } else {
      var stype = "2"
    }

    //把新的值给新的数组
    var newList = that.data.list
    newList[index].Default = stype
    //把新的数组传给前台
    that.setData({
      list: newList
    })
    //调用计算数目方法
    that.countNum()
    //计算金额
    that.count()
    that.choice(newList[index].Id)

  },
  choice: function (Id) {//选择
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + 'Car/choice',
      data: {
        // opt: 'choice',
        id: Id,
        // checkId: checkId,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
      }
    })
  },
  //加法
  addtion: function (e) {
    var that = this
    //得到下标
    var index = e.currentTarget.dataset.index
    //得到点击的值
    var num = e.currentTarget.dataset.num
    //默认99件最多
    if (num < 100) {
      num++
    }
    //把新的值给新的数组
    var newList = that.data.list
    newList[index].Count = num
    console.log(newList[index].Id,'车id')
    //把新的数组传给前台
    that.setData({
      list: newList
    })
    //调用计算数目方法
    that.countNum();
    //计算金额
    that.count();
    that.jia(newList[index].Id);
  },
  jia: function (Id) {//加
    var that = this;
 
    wx.request({
      url: app.globalData.apiUrl + 'Car/updateCart',
      data: {
        // opt: 'updateCart',
        id: Id,
        type: 1,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {

      }
    })
  },
  //减法
  subtraction: function (e) {
    var that = this
    //得到下标
    var index = e.currentTarget.dataset.index
    //得到点击的值
    var num = e.currentTarget.dataset.num
    //把新的值给新的数组
    var newList = that.data.list
    //当1件时，点击移除
    if (num == 1) {
      // newList.splice(index, 1)
    } else {
      num--
      newList[index].Count = num
    }

    //把新的数组传给前台
    that.setData({
      list: newList
    })
    //调用计算数目方法
    that.countNum()
    //计算金额
    that.count()
    that.jian(newList[index].Id);

  },
  jian: function (Id) {//减
    var that = this;
   
    wx.request({
      url: app.globalData.apiUrl + 'Car/updateCart',
      data: {
        // opt: 'updateCart',
        id: Id,
        type: 2,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {

      }
    })
  },
  //全选
  allSelect: function (e) {
    var that = this
    //先判断现在选中没
    var allSelect = e.currentTarget.dataset.select
    var newList = that.data.list
    if (allSelect == "circle") {
      //先把数组遍历一遍，然后改掉select值
      for (var i = 0; i < newList.length; i++) {
        newList[i].Default = "1"
      }
      var select = "success"
    } else {
      for (var i = 0; i < newList.length; i++) {
        newList[i].Default = "2"
      }
      var select = "circle"
    }
    that.setData({
      list: newList,
      allSelect: select
    })
    //调用计算数目方法
    that.countNum()
    //计算金额
    that.count()
    that.selection(select)

  },
  selection: function (select) {//全选
    var that = this;
    if (select == 'success') {
      var selection = 1;
    
    } else {
      var selection = 2;

    }
    wx.request({
      url: app.globalData.apiUrl + 'Car/allxuan',
      data: {
        // opt: 'selection',
        userId: wx.getStorageSync('userId'),
        selection: selection,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data != null) {
         
        }
      }
    })
  },
  //计算数量
  countNum: function () {
    var that = this
    //遍历数组，把既选中的num加起来
    var newList = that.data.list
    var allNum = 0
    for (var i = 0; i < newList.length; i++) {
      if (newList[i].Default == "1") {
        allNum += parseInt(newList[i].Count)
      }
    }
    parseInt
    console.log(allNum)
    that.setData({
      num: allNum
    })
  },
  //计算金额方法
  count: function () {
    var that = this
    //思路和上面一致
    //选中的订单，数量*价格加起来
    var newList = that.data.list
    var newCount = 0
    for (var i = 0; i < newList.length; i++) {
      if (newList[i].Default == "1") {
        newCount += newList[i].Count * newList[i].PriceVip
      }
    }
    that.setData({
      count: newCount
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var userId = wx.getStorageSync('userId');
    if (userId != null && userId > 0 && userId != '') {

      // that.getCountChecked(function(data){});//读取全选状态
    } else {
      wx.showModal({
        title: '温馨提示',
        content: '先登录',
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/login/login',
            })
          } else if (res.cancel) {
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getCartList();//读取购物车
    this.setData({
      allSelect: "circle",
    })
    
  },
  getCartList: function () {//读取购物车
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + 'Car/getCartList',
      data: {
        // opt: 'getCartList',
        userId: wx.getStorageSync('userId'),
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data != null) {
          that.setData({
            list: res.data.list,
            LevelId: res.data.LevelId
          })
        }
      }
    })
  },
  delCart: function (e) {//删除
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.request({
      url: app.globalData.apiUrl + 'Car/deleteCart',
      data: {
        // opt: 'deleteCart',
        id: id,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data != null) {
          if (res.data.status == 0) {
            that.getCartList();//读取购物车
          } else {
            wx.showToast({
              title: '失败',
              icon: 'error',
              duration: 2000
            })
          }
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  jiesuan:function(){
    var that = this;
    that.getCountChecked(function (data) {
      // console.log(data)
      if (data == 0) {
        wx.navigateTo({
          url: '/pages/con_order/con_order',
        })
      }else if (data == 1) {
        wx.showModal({
          title: '温馨提示',
          content: '请选择商品',
          success: function (res) {
            if (res.confirm) {

            } else if (res.cancel) {
            }
          }
        })
      }
    });
  
  },
  getCountChecked: function (callback) {//检查是否全选
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + 'Car/isxuanzhong',
      data: {
        // opt: 'getCountChecked',
        userId: wx.getStorageSync('userId'),
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        // console.log(res)
        callback(res.data.status)
      }
    })
  },
})